<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="com.hnas.core.util.PropertyUtil"%>
<%@page import="com.hnas.core.dto.DataBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>代金券列表</title>
<script type="text/javascript">
</script>
</head>
<body>
	<!-- 标题 -->
	<div class="titleBar m_b_20">
		<div class="tit fl b_l_e74a5f">
			<h3 class="cn">代金券列表</h3>
			<span class="en">coupon information</span>
		</div>
		<div class="fr"></div>
	</div>
	<!-- end 标题 -->

	<form method="post" action="${pageContext.request.contextPath }/backend/coupon/search" name="searchForm"  >
  	<input type="hidden" name="pageNo" value="${dto.pageNo}">
  	<input type="hidden" name="totalCount" value="${dto.totalCount}">
	<!-- 查询 -->
	<div class="box_03 p_20 m_b_20">
		<table class="tb_style02" width="100%">
	        <tr>
	            <th>每页显示</th>
	            <td colspan="5">
	            	<input style="width:50px;" class="input_style2" type="text" placeholder="" id="pageSize" name="pageSize" value="${dto.pageSize}"> 条
	                <span style="margin-left:170px;">
	                	<input style="width:110px;" class="btn_brown02 m_r_12" type="button" name="searchData" id="searchData" value="查  询">
	               		<input style="width:110px;" class="btn_brown02 m_r_12" type="button" name="createData" id="createData" value="添 加">
	                </span>
	            </td>
	         </tr>
		</table>
	</div>
	<!-- end 查询 -->
				
</form>

<div class="m_b_20">
	<table class="tb_style05" width="100%" style="table-layout:fixed;">
		<thead>
		<tr>
			<c:forEach items="${beans}" var="o">
			<c:if test="${o.display && !o.hidden}"><th>${o.title}</th></c:if>
			</c:forEach>
			<!-- <th width="100">操作</th> -->
		</tr>
		</thead>
		<tbody>
			<c:forEach items="${pg.list}" var="o">
			<tr>
				<c:forEach items="${beans}" var="o1">
					<c:if test="${o1.display && !o1.hidden}">
					<td style="word-wrap:break-word;"><%=String.valueOf(PropertyUtil.getValue(pageContext.getAttribute("o"),((DataBean)pageContext.getAttribute("o1")).getField())).replace("false","否").replace("true","是")%></td></c:if>
				</c:forEach>
					<%-- <td>
						<a href="#" class="c_3366cc btn disableData" id="${o.id }">启用</a>&nbsp;&nbsp;
						<a href="#" class="c_3366cc btn deleteData" id="${o.id }">删除</a>
					</td> --%>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<div class="searpage" align="right"><div>${pg.pageStr}</div></div><br/>
</div>
<!-- 添加start -->
<div class="createDataDiv" id="createDiv" title="生成代金券" style="display: none;" >
	<form action="#" id="createFm" method="post" name="createFm" class="editFm" >
		<input type="hidden" id="dealId" name = "id" value=""/>
		<table >
  			<tr>
 				<td width="25%" align="left"> 
 					<label>代金券类型：</label>
 				</td>
 				<td><select id="couponTypeSelect" name="couponType" style="width: 100px;" class="select ui-widget-content ui-corner-all input_style2">
	 					<c:forEach items="${couponTypeList }" var="o">
	 						<option value="${o.id }" >${o.couponTypeName }</option>
	 					</c:forEach>
	 				</select>
 				</td>  
  			</tr>
			<tr>
 				<td width="25%" align="left"> 
 					<label>代金券个数：</label>
 				</td>
 				<td><input type="text" id="countInput" name="count" style="width:100px;">
 				<font color="red"> *</font></td> 
  			</tr>
  			
  			<tr>
 				<td width="25%" align="left"> 
 					<label>有效起始时间：</label>
 				</td>
               	<td>
               		<input type="text" style="width:160px;" name="validBeginTime" id="validBeginTime" style="width:100px;" class="input_style2"/>
                </td>
  			</tr>
  			<tr>
 				<td width="25%" align="left"> 
 					<label>有效截止时间：</label>
 				</td>
               	<td>
               		<input type="text" style="width:160px;" name="validEndTime" id="validEndTime" style="width:100px;" class="input_style2"/>
                </td>
  			</tr>
  			
		</table>
	</form>
</div>
<!-- 添加end -->
<script>
	function gotoJumpPage(){
		var jumpNum= $("#jumpNum").val();
		$("#search_page_size").val($("#id_page_size").val());
		gotoPage(jumpNum);
		return false;
	}
	function deleteData(id){
		var deleteURL = "${pageContext.request.contextPath }/backend/coupon/del?id=" + id;
		alert(deleteURL);
	     $.ajax({
				url:deleteURL,
				type: 'POST',
				error: function(XMLHttpRequest, textStatus, errorThrown){
					//alert('加载数据异常。' + textStatus + "/" + errorThrown);
					$.dialog.alert({title:"删除",content:"加载数据异常"});
				},
				success: function(data){
					if(data.result==1) {
						//alert("删除成功");
						$.dialog.alert({title:"删除",content:"删除成功"});
						/** 删除后触发查询 **/
						query();
					} else {
						//alert("删除失败"+data.errorMessage);
						$.dialog.alert({title:"删除",content:"删除失败"+data.errorMessage});
					}
				}
			});
	}
	$(function(){
		$("#searchData").click(function(){
			/** 校验输入页数是否非法  **/
			var pageSize=$("#pageSize").val();
			var reg = new RegExp("^[0-9]*$");
			if($.trim(pageSize)!=""){
				if(!reg.test($.trim(pageSize))){
					alert("每页显示数请输入非负整数");
	 				return false;
				}
			}
			if($.trim(pageSize)==""){
				alert("每页显示数不应许为空");
				return false;
			}
			doSubmit();
		});
		
		/** 添加 **/	 
		$("#createData").click(function(){
			doReset(document.createFm);
			$("#createDiv").dialog("open");
		});
		
		/** 删除 **/
		$(".deleteData").click(function(){
			var id = $(this).attr("id");
			 $.dialog.confirm({title:"删除",content:"您确定要删除吗",callback:"deleteData('"+id+"');"});
		});
		$("#createDiv").dialog({
			autoOpen : false,
			bgiframe: true, 
			height : 300,
			width : 400,
			modal : true,
			resizable :false,
	       	hide: '',
	        show:'',
	        zIndex:1000,
			buttons : {
				"保 存" : function() {
					var bValid = doValid();
					if(bValid){
						//var isAdd = ($("#dealId").val() == null) || ($.trim($("#dealId").val()) == "")||($.trim($("#dealId").val()) == "0");
						var createUrl = "${pageContext.request.contextPath }/backend/coupon/create";
						$.ajax({
							url:createUrl,
							type: 'POST',
							data: $("#createFm").serialize(),
							error: function(XMLHttpRequest, textStatus, errorThrown){
							//alert('保存异常。' + textStatus + "/" + errorThrown);
							 $.dialog.alert({title:"保存",content:"保存异常"});
							},
							success: function(data){
								if(data.result==1) {
									$("#createDIV").dialog("close");
									$.dialog.alert({title:"保存",content:"创建成功"});
									query();
								} else {
									$.dialog.alert({title:"保存",content:"创建失败："+ data.errorMessage});	
								}
							}
						});  
					}
				}, 
				"取 消" : function() {
					$(this).dialog("close");			
				}
			},
			close : function() {
				$(this).dialog("close");
			}
		});
		
		function doValid(){
			var count = $("#countInput").val();
			if(count == null || $.trim(count) == ''){
				$.dialog.alert({title:"错误",content:"代金券个数不能为空！",objCtrl:"#countInput"});
				return false;
			}else{
				var patrn = /^\d+(\.\d+)?$/;
	            if (!patrn.exec(count)) {
	                    $.dialog.alert({title:"错误",content:"代金券个数必须是数字！",objCtrl:"#countInput"});
	    				return false;
	            }
			}
			var couponType = $("#couponTypeSelect").val();
			if(couponType == null || $.trim(couponType) == ''){
				$.dialog.alert({title:"错误",content:"代金券类型不能为空！",objCtrl:""});
				return false;
			}
			return true;
		}
		
		$("#validBeginTime").datepicker({
			dateFormat:'yy-mm-dd',
			changeYear: true,
			changeMonth: true,
			yearRange: "1900:2049" 
		});
		$("#validEndTime").datepicker({
			dateFormat:'yy-mm-dd',
			changeYear: true,
			changeMonth: true,
			yearRange: "1900:2049" 
		});
	});
</script>
</body>
</html>